﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using DTO;

namespace DAO
{
    public class HoaDonDAO
    {
        private SqlConnection con;
        private DataProvider dp;

        public HoaDonDAO()
        {
            this.dp = new DataProvider();
        }

        public List<HoaDonInfo> LayDanhSachHoaDon()
        {
            List<HoaDonInfo> ds = new List<HoaDonInfo>();
            this.con = dp.MoKetNoi();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = this.con;
            cmd.CommandText = "sp_DanhSachHoaDon";
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataReader rd = cmd.ExecuteReader();
            while (rd.Read())
            {
                HoaDonInfo h = new HoaDonInfo();
                h.Phong = new PhongDTO();
                h.Phong.Tenphong = rd.GetString(0);
                h.NgayBatDau = rd.GetDateTime(1);
                h.NgayKetThuc = rd.GetDateTime(2);
                h.DonGia = (double)rd.GetDecimal(3);
                ds.Add(h);
            }
            dp.DongKetNoi(this.con);
            return ds;
        }

        public bool ThemHoaDon(int MaPhieuThue, double TongTien, DateTime KetThuc)
        {
            this.con = dp.MoKetNoi();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = this.con;
            cmd.CommandText = "sp_ThemHoaDon";
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter param = cmd.Parameters.Add("@MaPhieuThue", SqlDbType.Int);
            param.Value = MaPhieuThue;
            param = cmd.Parameters.Add("@TongTien", SqlDbType.Money);
            param.Value = (decimal)TongTien;
            param = cmd.Parameters.Add("@KetThuc", SqlDbType.DateTime);
            param.Value = KetThuc;
            if (cmd.ExecuteNonQuery() > 0) {
                dp.DongKetNoi(this.con);
                return true;
            }
            dp.DongKetNoi(this.con);
            return false;
        }
    }
}
